home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Leser 15 / Amiga Plus Leser CD 15.iso / Tools / Development / MosaicSRC / libwww2 / Unused / HTAAFile.c next >
Encoding:
C/C++ Source or Header  |  2002-03-13  |  5.1 KB  |  200 lines

  1.  
  2. /* MODULE                            HTAAFile.c
  3. **        FILE ROUTINES FOR AUTHENTICATION
  4. **        (PASSWD AND GROUP FILES) AND
  5. **        ACCESS CONTROL LIST (.www_acl)
  6. ** AUTHORS:
  7. **    AL    Ari Luotonen    luotonen@dxcern.cern.ch
  8. **
  9. ** HISTORY:
  10. **
  11. **
  12. ** BUGS:
  13. **
  14. **
  15. */
  16.  
  17.  
  18. #include <stdio.h>        /* FILE */
  19. #include <string.h>
  20. #include "tcp.h"        /* Macro FROMASCII() */
  21. #include "HTAAUtil.h"        /* Common utilities used in AA */
  22. #include "HTAAFile.h"        /* Implemented here */
  23.  
  24.  
  25. #define SPACE            ' '
  26. #define TAB            '\t'
  27.  
  28.  
  29.  
  30. /* PUBLIC                        HTAAFile_nextRec()
  31. **            GO TO THE BEGINNING OF THE NEXT RECORD
  32. ** ON ENTRY:
  33. **    fp    is the file from which records are read from.
  34. **
  35. ** ON EXIT:
  36. **    returns    nothing. File read pointer is located at the beginning
  37. **        of the next record.
  38. **
  39. */
  40. PUBLIC void HTAAFile_nextRec ARGS1(FILE *, fp)
  41. {
  42.     int ch = getc(fp);
  43.  
  44.     while (ch != EOF  &&  ch != CR  &&  ch != LF)
  45.     ch = getc(fp);            /* Skip until end-of-line */
  46.  
  47.     while (ch != EOF &&
  48.        (ch == CR  ||  ch == LF))    /*Skip carriage returns and linefeeds*/
  49.     ch = getc(fp);
  50.  
  51.     if (ch != EOF)
  52.     ungetc(ch, fp);
  53. }
  54.  
  55.  
  56. /* PRIVATE                            read_item()
  57. **        READ AN ITEM FROM A PASSWORD, GROUP
  58. **        OR ACCESS CONTROL LIST FILE
  59. **        i.e. either a field, or a list item.
  60. ** ON ENTRY:
  61. **    fp        is the file to read the characters from
  62. **    contents    is the character array to put the characters
  63. **    reading_list    if TRUE, read a list item (ends either in
  64. **            acomma or acolon),
  65. **            if FALSE, read a field (ends in acolon).
  66. **    max_len        is the maximum number of characters that may
  67. **            be read (i.e. the size of dest minus one for
  68. **            terminating null).
  69. ** ON EXIT:
  70. **    returns        the terminating character
  71. **            (i.e. either separator or CR or LF or EOF).
  72. **    contents    contains a null-terminated string representing
  73. **            the read field.
  74. ** NOTE 1:
  75. **            Ignores leading and trailing blanks and tabs.
  76. ** NOTE 2:
  77. **            If the item is more than max_len characters
  78. **            long, the rest of the characters in that item
  79. **            are ignored.  However, contents is always
  80. **            null-terminated!
  81. */
  82. PRIVATE int read_item ARGS4(FILE *,    fp,
  83.                 char *,    contents,
  84.                 BOOL,    reading_list,
  85.                 int,    max_len)
  86. {
  87.     char * dest = contents;
  88.     char * end = contents;
  89.     int cnt = 0;
  90.     int ch = getc(fp);
  91.  
  92.     while (SPACE == ch || TAB == ch)    /* Skip spaces and tabs */
  93.     ch = getc(fp);
  94.  
  95.     while (ch != FIELD_SEPARATOR &&
  96.        (!reading_list || ch != LIST_SEPARATOR) &&
  97.        ch != CR  &&  ch != LF  &&  ch != EOF  &&  cnt < max_len) {
  98.     *(dest++) = ch;
  99.     cnt++;
  100.     if (ch != SPACE && ch != TAB)
  101.         end = dest;
  102.     ch = getc(fp);
  103.     } /* while not eol or eof or too many read */
  104.  
  105.     /* Terminate the string, truncating trailing whitespace off.
  106.     ** Otherwise (if whitespace would be included), here would
  107.     ** be *dest='\0'; and  cnt -= ... would be left out.
  108.     */
  109.     *end = '\0';
  110.     cnt -= dest-end;
  111.  
  112.     if (cnt == max_len)    {
  113.     /* If the field was too long (or exactly maximum) ignore the rest */
  114.     while (ch != FIELD_SEPARATOR &&
  115.            (!reading_list || ch != LIST_SEPARATOR) &&
  116.            ch != CR  &&  ch != LF  &&  ch != EOF)
  117.         ch = getc(fp);
  118.     }
  119.  
  120.     if (ch == CR || ch == LF)
  121.     ungetc(ch, fp);    /* Push back the record separator (NL or LF) */
  122.  
  123.     return ch;        /* Return the terminating character */
  124. }
  125.  
  126.  
  127.  
  128. /* PUBLIC                        HTAAFile_readField()
  129. **        READ A FIELD FROM A PASSWORD, GROUP
  130. **        OR ACCESS CONTROL LIST FILE
  131. **        i.e. an item terminated by colon,
  132. **        end-of-line, or end-of-file. 
  133. ** ON ENTRY:
  134. **    fp        is the file to read the characters from
  135. **    contents    is the character array to put the characters
  136. **    max_len        is the maximum number of characters that may
  137. **            be read (i.e. the size of dest minus one for
  138. **            terminating null).
  139. ** ON EXIT:
  140. **    returns        the terminating character
  141. **            (i.e. either separator or CR or LF or EOF).
  142. **    contents    contains a null-terminated string representing
  143. **            the read field.
  144. ** NOTE 1:
  145. **            Ignores leading and trailing blanks and tabs.
  146. ** NOTE 2:
  147. **            If the field is more than max_len characters
  148. **            long, the rest of the characters in that item
  149. **            are ignored.  However, contents is always
  150. **            null-terminated!
  151. */
  152. PUBLIC int HTAAFile_readField ARGS3(FILE *, fp,
  153.                     char *, contents,
  154.                     int,    max_len)
  155. {
  156.     return read_item(fp, contents, NO, max_len);
  157. }
  158.  
  159.  
  160.  
  161.  
  162. /* PUBLIC                        HTAAFile_readList()
  163. **
  164. **            READ A LIST OF STRINGS SEPARATED BY COMMAS
  165. **            (FROM A GROUP OR ACCESS CONTROL LIST FILE)
  166. ** ON ENTRY:
  167. **    fp        is a pointer to the input file.
  168. **    result        is the list to which append the read items.
  169. **    max_len        is the maximum number of characters in each
  170. **            list entry (extra characters are ignored).
  171. ** ON EXIT:
  172. **    returns        the number of items read.
  173. **
  174. */
  175. PUBLIC int HTAAFile_readList ARGS3(FILE *,    fp,
  176.                    HTList *,    result,
  177.                    int,        max_len)
  178. {
  179.     char *item = NULL;
  180.     char terminator;
  181.     int cnt = 0;
  182.  
  183.     do {
  184.     if (!item  &&  !(item = (char*)malloc(max_len+1)))
  185.         outofmem(__FILE__, "HTAAFile_readList");
  186.     terminator = read_item(fp, item, YES, max_len);
  187.     if (strlen(item) > 0) {
  188.         cnt++;
  189.         HTList_addObject(result, (void*)item);
  190.         item = NULL;
  191.     }
  192.     } while (terminator != FIELD_SEPARATOR  &&
  193.          terminator != CR  &&  terminator != LF  &&
  194.          terminator != EOF);
  195.  
  196.     if (item) free(item);    /* This was not needed */
  197.     return cnt;
  198. }
  199.  
  200.